package com.example.othree.controller;

import com.example.othree.common.R;
import com.example.othree.entity.MainCrops;
import com.example.othree.service.MainCropsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangmiaohang
 * @since 2024-11-24
 */
@RestController
@CrossOrigin
@RequestMapping("/api")
public class MainCropsController {
    @Autowired
    private MainCropsService mainCropsService;

    @GetMapping("/crops-plant-data")
    @ApiOperation("获取邢台市或衡水市的种产空间数据")
    public R CropsDataForCrop(@RequestParam("city") String city,
                              @RequestParam("year") String year,
                              @RequestParam("crop") String crop){
        HashMap<String, Object> result = new HashMap<>();
        try {
            List<MainCrops> data = mainCropsService.cropsData(city, year, crop);

            // 利用流操作从用户列表中提取数据，并构建HashMap
            Map<String, Integer> ProductMap = data.stream()
                    .filter(mainCrops -> mainCrops.getCounty() != null && mainCrops.getTotalProduct() != null)
                    .collect(Collectors.toMap(MainCrops::getCounty, MainCrops::getTotalProduct));
            if (!ProductMap.isEmpty()) result.put("总产量", ProductMap);

            Map<String, Integer> AreaMap = data.stream()
                    .filter(mainCrops -> mainCrops.getCounty() != null && mainCrops.getSowArea() != null)
                    .collect(Collectors.toMap(MainCrops::getCounty, MainCrops::getSowArea));
            if (!AreaMap.isEmpty()) result.put("种植面积", AreaMap);

            Map<String, Integer> UnitMap = data.stream()
                    .filter(mainCrops -> mainCrops.getCounty() != null && mainCrops.getUnitProduct() != null)
                    .collect(Collectors.toMap(MainCrops::getCounty, MainCrops::getUnitProduct));
            if (!UnitMap.isEmpty()) result.put("单产量", UnitMap);


            Map<String, Integer> OutputMap = data.stream()
                    .filter(mainCrops -> mainCrops.getCounty() != null && mainCrops.getOutputValue() != null)
                    .collect(Collectors.toMap(MainCrops::getCounty, MainCrops::getOutputValue));
            if (!OutputMap.isEmpty())
                result.put("产值", OutputMap);


        }catch (DataAccessException e){
            e.printStackTrace();
            return R.error().message("数据库访问出错");
        } catch (Exception e){
            e.printStackTrace();
            return R.error().message("出现了未知错误");
        }

        return R.ok().data("data", result);
    }

    @GetMapping("/crops-day-data")
    @ApiOperation("获取邢台或衡水某区县的农作物种产逐日数据")
    public R CropsDataForCrop(@RequestParam("city") String city,
                              @RequestParam("startYear") String startYear,@RequestParam("endYear") String endYear,
                              @RequestParam("crop") String crop,
                              @RequestParam("district") String district){
        HashMap<String, Object> result = new HashMap<>();
        try {
            List<MainCrops> dataList = mainCropsService.cropsDataForYear(city, district, startYear, endYear, crop);
            System.out.println(dataList);

            for (MainCrops i:
                    dataList) {
                HashMap<String, Object> data = new HashMap<>();

                data.put("种植面积", i.getSowArea());
                data.put("单产", i.getUnitProduct());
                data.put("总产量", i.getTotalProduct());
                data.put("产值", i.getOutputValue());

                result.put(i.getYear(), data);
            }
        }catch (DataAccessException e){
            e.printStackTrace();
            return R.error().message("数据库访问出错");
        } catch (Exception e){
            e.printStackTrace();
            return R.error().message("出现了未知错误");
        }

        return R.ok().data("data", result);
    }
}

